﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Linq;
using System.Xml.XPath;
using ExtremeML.Spreadsheet;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Sdk.Vml
{
	/// <summary>
	/// Implements a Vml shape for use with worksheet comments.
	/// </summary>
	internal class CommentShapeCollection
	{
		public CommentShapeCollection(
			VmlDrawing parent)
		{
			if (parent == null) throw new ArgumentNullException("parent");

			Parent = parent;
		}

		/// <summary>
		/// Gets the parent <see cref="Drawing"/> instance.
		/// </summary>
		public VmlDrawing Parent { get; private set; }

		/// <summary>
		/// Returns the comment shape that corresponds to the specified cell address.
		/// </summary>
		/// <param name="worksheet">The <see cref="WorksheetWrapper"/> that contains the worksheet targeted by the associated comment.</param>
		/// <param name="cell">A <see cref="GridReference"/> that specifies the location of the associated cell.</param>
		/// <returns>An existing <see cref="CommentShape"/> instance or <c>null</c> if no match is found.</returns>
		public CommentShape GetShape(
			WorksheetWrapper worksheet,
			GridReference cell)
		{
			if (worksheet == null) throw new ArgumentNullException("worksheet");
	
			var query = string.Format("./v:shape[x:ClientData[x:Row='{0}' and x:Column='{1}']]", cell.RowIndex, cell.ColumnIndex);
			var item = Parent.Document.Root.XPathSelectElement(query, Utility.Manager);
			return item == null ? null : new CommentShape(item, worksheet, cell);
		}

		/// <summary>
		/// Creates and returns a new comment shape.
		/// </summary>
		/// <param name="worksheet">The <see cref="WorksheetWrapper"/> that contains the worksheet targeted by the associated comment.</param>
		/// <returns>An existing or newly created <see cref="CommentShape"/> instance.</returns>
		public CommentShape GetShape(
			WorksheetWrapper worksheet)
		{
			if (worksheet == null) throw new ArgumentNullException("worksheet");

			Parent.EnsureCommentShapeType();
			Parent.EnsureCommentShapeLayout();
			var shape = new CommentShape(worksheet);
			Parent.Document.Root.Add(shape.Element);
			return shape;
		}

		/// <summary>
		/// Returns the number of comment shape elements in the VmlDrawing.
		/// </summary>
		/// <returns>The number of comment shape elements.</returns>
		public int Count()
		{
			return Parent.Document.Root.XPathSelectElements("./v:shape[x:ClientData]", Utility.Manager).Count();
		}
	}
}